package org.community.detail.controller;


import entity.CommonResult;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.community.detail.service.media.picture.PortraitMainService;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

@RestController
@Data
@Slf4j
public class PortraitController {

    @Resource
    private PortraitMainService service;

    @PostMapping(value = "/portrait/{userId}",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Object portraitUpload(@PathVariable("userId") Integer userId, MultipartFile file){

        boolean result = this.getService().portraitUpload(userId,file);

        if(result){
            return new CommonResult<>(200,"上传成功！",null);
        }

        return new CommonResult<>(400,"上传失败！",null);
    }



    @GetMapping("/portrait/{userId}/{username}")
    public ResponseEntity<byte[]> getPortrait(@PathVariable("userId") Integer userId,@PathVariable("username") String username) throws IOException {
        log.info("getPortrait开始执行");
        return this.getService().portraitDownload(userId,username);
    }
}
